var _pqUtil = {};

_pqUtil.f = {
    getData: function ($grid) {
        return $grid.pqGrid("option").dataModel.data;
    },
    getTotalCount: function ($grid) {
        return $grid.pqGrid("option").dataModel.data.length;
    },
    /**
     * 修改表格列数据
     * @param $grid
     * @param colM
     */
    setColModel: function ($grid, colM) {
        $grid.pqGrid("option", "colModel", colM);
    },
    /**
     * 修改表格样式
     * @param $grid
     * @param formulas
     */
    setFormulas: function ($grid, formulas) {
        $grid.pqGrid("option", "formulas", formulas);
    },
    /**
     * 修改表格title
     * @param $grid
     * @param title
     */
    seTitle: function ($grid, title) {
        if (title != null && title != "" && title != undefined) {
            $grid.pqGrid("option", "title", title);
        }
    },
    /**
     * 是否显示表格title
     * @param $grid
     * @param title
     */
    isShowTitle: function ($grid, isShow) {
        $grid.pqGrid("option", "showTitle", isShow);
    },
    /**
     * 销毁表格
     * @param $grid
     */
    destroy: function ($grid) {
        $grid.pqGrid("destroy");
    },
    /**
     * 加载PqGrid数据
     * @param $grid
     * @param gridData
     */
    loadPqGridData: function ($grid, gridData) {
        _pqUtil.f.cleanAndResetGrid($grid);
        $grid.pqGrid('option', 'dataModel.data', gridData);
        $grid.pqGrid("refreshDataAndView");
        _pqUtil.f.updateToolBar($grid, gridData.length);
    },
    /**
     * 清空
     * @param $grid
     */
    cleanGrid: function ($grid) {
        $grid.pqGrid("option", "dataModel.data", []);
        $grid.pqGrid("refreshDataAndView");
    },
    /**
     * 清空
     * @param $grid
     */
    cleanAndResetGrid: function ($grid) {
        console.log('pqgrid - cleanAndResetGrid');
        try {
            _pqUtil.f.cleanGrid($grid);
            _pqUtil.f.updateToolBar($grid, 0);
            $grid.pqGrid("reset", {sort: true, filter: true});
        } catch (e) {
            console.warn('pqgrid - cleanAndResetGrid.  error catch.');
        }
    },
    /**
     * 更新数量
     * @param $grid
     * @param total
     */
    updateToolBar: function ($grid, total) {
        console.log('pqgrid - updateToolBar');
        var realTotal, toolbar;
        if (total) {
            realTotal = total;
        } else {
            realTotal = _pqUtil.f.getTotalCount($grid);
        }

        toolbar = $grid.pqGrid("option", "toolbar");
        toolbar.items[1].type = "<span>共 " + realTotal + " 条</span>";
        $grid.pqGrid("refreshToolbar");
    },
    /**
     * 显示/隐藏列
     *
     * @param $grid
     * @param showColModels
     * @param hideColModels
     */
    showHideColumns: function ($grid, showColModels, hideColModels) {
        var i, leni,
            j, lenj,
            k, lenk,
            curDataIndx,
            allColumns = $grid.pqGrid("getColModel");

        for (i = 0, leni = allColumns.length; i < leni; i++) {
            curDataIndx = allColumns[i]['dataIndx'];
            for (j = 0, lenj = showColModels.length; j < lenj; j++) {
                if (curDataIndx === showColModels[j]['dataIndx']) {
                    showColModels[j].hidden = false;
                    break;
                }
            }
            if (j === lenj) {
                for (k = 0, lenk = hideColModels.length; k < lenk; k++) {
                    if (curDataIndx === hideColModels[k]['dataIndx']) {
                        hideColModels[k].hidden = true;
                        break;
                    }
                }
            }
        }
        console.log('pqgrid - showHideColumns');
    },
    createGrid: function ($grid, conf) {
        var defConf = {
            width: 'auto',
            height: '100%',
            postRenderInterval: -1, //synchronous post rendering.
            showTitle: true,
            title: "",
            resizable: true,
            hwrap: false,
            wrap: false,
            dragColumns: {enabled: false},
            numberCell: {
                show: false,
                width: 50
            },
            showBottom: false,
            showTop: true,
            showToolbar: true,
            virtualY: true,
            hoverMode: 'row',
            toolbar: {
                items: [
                    {
                        type: 'button',
                        width: 150,
                        label: "导出Excel",
                        icon: 'ion-ios-download-outline',
                        listener: function () {
                            // var format = 'xls',
                            //     blob = this.exportData({
                            //         format: format,
                            //         render: true
                            //     });
                            // if (typeof blob === "string") {
                            //     blob = new Blob([blob]);
                            // }
                            var format = 'xlsx',
                                blob = this.exportData({
                                format: format,
                                render: true,
                                type: 'blob'
                            });
                            var excel_name = "Report_Excel.";
                            if (this.options.title != null && this.options.title != "" && this.options.title != 'undefined'){
                                excel_name = this.options.title + ".";
                            }
                            saveAs(blob, excel_name + format);
                        }
                    },{
                        type: "<span>共 0 条</span>",
                        width: 110
                    },
                ]
            },
            scrollModel: {
                autoFit: false
            },
            collapsible: {
                on: false,
                collapsed: false,
                toggle: false,
            },
            colModel: [],
            dataModel: {
                data: [],
                location: "local"
            }
        };
    $grid.pqGrid($.extend({}, defConf, conf));
  }
};